616C - The Labyrinth - CodeForces Solution


dfs and similar *1600

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
int t,n,m,cnt,vis[1010][1010],num[1000010],ans[1010][1010];
char ma[1010][1010];
int dfs(int x,int y,int id){
	if(vis[x][y]==id||ma[x][y]!='.')return 0;
	else vis[x][y]=id;
	return 1+dfs(x-1,y,id)+dfs(x+1,y,id)+dfs(x,y-1,id)+dfs(x,y+1,id);
}
int cal(int a,int b,int c,int d){
	int sum=1;
	sum+=num[a];
	if(b!=a) sum+=num[b];
	if(c!=b&&c!=a)sum+=num[c];
	if(d!=c&&d!=b&&d!=a)sum+=num[d];
	return sum%10;
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;++i)cin>>ma[i]+1;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
			if(ma[i][j]=='.'&&vis[i][j]==0)++cnt,num[cnt]=dfs(i,j,cnt);
	for(int i=1;i<=n;++i) {
		for(int j=1;j<=m;++j)
			if(ma[i][j]=='.') cout<<'.';
			else cout<<cal(vis[i-1][j],vis[i+1][j],vis[i][j-1],vis[i][j+1]);
		cout<<endl;
	}

	return 0;
}

			  				 		 		            	  	


Comments

Submit
0 Comments
More Questions

253A - Boys and Girls
1327E - Count The Blocks
984A - Game
12B - Correct Solution
1355B - Young Explorers
485A - Factory
628A - Tennis Tournament
1436B - Prime Square
1707B - Difference Array
1422C - Bargain
1611F - ATM and Students
660A - Co-prime Array
1692F - 3SUM
1470A - Strange Birthday Party
190D - Non-Secret Cypher
1721B - Deadly Laser
1721C - Min-Max Array Transformation
1721A - Image
1180C - Valeriy and Deque
557A - Ilya and Diplomas
1037D - Valid BFS
1144F - Graph Without Long Directed Paths
1228A - Distinct Digits
355B - Vasya and Public Transport
1230A - Dawid and Bags of Candies
1530A - Binary Decimal
1472D - Even-Odd Game
441C - Valera and Tubes
1328E - Tree Queries
265A - Colorful Stones (Simplified Edition)